package com.java47.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.java47.mapper.ItemMapper;
import com.java47.mapper.OptionMapper;
import com.java47.mapper.SubjectMapper;
import com.java47.mapper.UserMapper;
import com.java47.pojo.Item;
import com.java47.pojo.Option;
import com.java47.pojo.Subject;
import com.java47.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper umapper;
    @Autowired
    private SubjectMapper smapper;
    @Autowired
    private OptionMapper omapper;
    @Autowired
    private ItemMapper imapper;

    @Override
    public Users selectOne(String uname) {
        return umapper.selectOne(uname);
    }

    @Override
    public List<Subject> show() {
        return smapper.show();
    }

    @Override
    public Subject showOne(String sid) {
        return smapper.selectOne(sid);
    }

    @Override
    public List<Option> showOlist(String sid) {
        return omapper.showOlist(sid);
    }

    @Override
    public boolean selectItem(String uid, String sid){
        return imapper.selectItem(uid,sid)==null?true:false;
    }

    @Override
    public boolean dd(Item item) {
        boolean b = false;
        Item it = imapper.dd(item);
        if(!it.getId().equals("")&&it.getId()!=null){
            b=true;
        }
        return b;
    }

    @Override
    @Transactional
    public boolean update(Map<String, Object> map) {
        int num = 0;
        if (map.keySet().iterator().hasNext()) {
            if (map.keySet().contains("add")) {
                num = imapper.add((Item) map.get("add"));
            }else if (map.keySet().contains("update")) {
                num = omapper.update((Item) map.get("update"));
            }else if(map.keySet().contains("addOption")){
                num = omapper.addOption((Option) map.get("addOption"));
            }else if(map.keySet().contains("addSubject")){
                num = smapper.addSubject((String) map.get("addSubject"));
            }else if(map.keySet().contains("delSubject")){
                num = smapper.del((String)map.get("delSubject"));
            }else if (map.keySet().contains("delItem")){
                num = imapper.delItem((String)map.get("delItem"));
            }else if (map.keySet().contains("delOption")){
                num = omapper.delOption((String)map.get("delOption"));
            }else if (map.keySet().contains("updateOption")) {
                num = omapper.updateOption((Option) map.get("updateOption"));
            }else if(map.keySet().contains("deleteItem")){
                num = imapper.deleteItem((Item)map.get("deleteItem"));
            }else if(map.keySet().contains("deleteOption")){
                num = omapper.deleteOption((String)map.get("deleteOption"));
            }
        }
        return num > 0 ? true : false;
    }

    @Override
    public Page showPage(Integer index, Integer size, String title) {
        Page<Object> page = PageHelper.startPage(index,size);
        List<Subject> slist = smapper.showSlist(title);
        return page;
    }
}
